import { $planet_AudioManager_poof$ } from "../../script/src/manager/audio-manager";
import { $planet_DataManager_poof$ } from "../ALIB/lib.data";
import { $planet_data_trigger_poof$ } from "./data.global.trigger";

export default class $planet_GlobalDC_poof$ {
	private static $planet__trigger__poof$: typeof $planet_data_trigger_poof$.ins;
	//private static $planet__clock__poof$: typeof $planet_data_clock_poof$.ins;

	public static get $planet_trigger_poof$(){
		return $planet_GlobalDC_poof$.$planet__trigger__poof$;
	}
	//public static get $planet_clock_poof$(){
	//	return $planet_GlobalDC_poof$.$planet__clock__poof$;
	//}

	public static $planet_init_poof$(){
		$planet_GlobalDC_poof$.$planet__trigger__poof$ = $planet_DataManager_poof$.$planet_initProxy_poof$($planet_data_trigger_poof$.name, $planet_data_trigger_poof$.ins, $planet_data_trigger_poof$.save, $planet_data_trigger_poof$.blocker);
		//$planet_GlobalDC_poof$.$planet__clock__poof$ = $planet_DataManager_poof$.$planet_initProxy_poof$($planet_data_clock_poof$.name, $planet_data_clock_poof$.ins, $planet_data_clock_poof$.save, $planet_data_clock_poof$.blocker, $planet_APP_NAME_poof$);
		//$planet_GlobalDC_poof$.$planet_clockRun_poof$();
		$planet_GlobalDC_poof$.$planet_listening_poof$();
	}

	private static $planet_listening_poof$(){
		$planet_DataManager_poof$.$planet_follow_poof$($planet_GlobalDC_poof$.$planet_trigger_poof$, '$planet_CLICK_poof$', (o, v)=>{
			$planet_AudioManager_poof$.$planet_ins_poof$.$planet_playEffect_poof$($planet_AudioManager_poof$.$planet_EFFECT_poof$.$planet_CLICK_poof$);
		}, false);
		let interval_stamp = Date.now();
		let td = 0;
		//$planet_DataManager_poof$.$planet_follow_poof$($planet_GlobalDC_poof$.$planet_clock_poof$, '$planet_current_time_poof$', (o, v)=>{
		//	td = v - interval_stamp;
		//	interval_stamp = v;
		//	$planet_GlobalDC_poof$.$planet__interval_call_token_map__poof$.forEach(async item=>{
		//		if((v - item.timestamp) >= item.interval){
		//			item.timestamp += item.interval;
		//			Reflect.apply(item.call, item.that, [td, v]);
		//		}
		//	});
		//}, false);

		//$planet_GlobalDC_poof$.$planet_interval_poof$(60 * 1000, (td, now)=>{
		//	if(abd.$planet_time_poof$.$planet_sameDay_poof$($planet_GlobalDC_poof$.$planet_clock_poof$.$planet_login_time_poof$, now)) return void 0;

		//	$planet_GlobalDC_poof$.$planet_clock_poof$.$planet_login_time_poof$ = now;
		//	$planet_GlobalDC_poof$.$planet_trigger_poof$.$planet_NEW_DAY_poof$ = now;
		//});

		while($planet_GlobalDC_poof$.$planet_follow_cache_poof$.length>0){
			let [pxi_uri, key, call, update, np] = $planet_GlobalDC_poof$.$planet_follow_cache_poof$.pop();
			//@ts-ignore
			$planet_GlobalDC_poof$.$planet_follow_poof$(pxi_uri, key, call, update).then(reg=>np.$planet_resolve_poof$(reg));
		}
	}

	//private static $planet_clockRun_poof$(){

	//	let now = Date.now();
	//	if(!abd.$planet_time_poof$.$planet_sameDay_poof$($planet_GlobalDC_poof$.$planet_clock_poof$.$planet_login_time_poof$, now)){
	//		$planet_GlobalDC_poof$.$planet_clock_poof$.$planet_login_time_poof$ = now;
	//		$planet_GlobalDC_poof$.$planet_trigger_poof$.$planet_NEW_DAY_poof$ = now;
	//	}

	//	setInterval(()=>$planet_GlobalDC_poof$.$planet_clock_poof$.$planet_current_time_poof$ = Date.now(), 1000/30);
	//}

	private static $planet_follow_cache_poof$: [string, string, Function, boolean, abd.$planet_NudityPromise_poof$<ReturnType<typeof $planet_DataManager_poof$.$planet_follow_poof$>>][] = [];
	public static $planet_follow_poof$<
		P extends Exclude<abd.$planet_OmitKeys_poof$<typeof $planet_GlobalDC_poof$, Function>, 'prototype'>,
		D extends typeof $planet_GlobalDC_poof$[P],
		K extends abd.$planet_OmitKeys_poof$<D, Function>
	>(pxi_uri: P, key: K, call: (o: D[K], v: D[K])=>void, update?: boolean){
		let pxi = Reflect.get($planet_GlobalDC_poof$, pxi_uri);
		let np = new abd.$planet_NudityPromise_poof$<ReturnType<typeof $planet_DataManager_poof$.$planet_follow_poof$>>();
		update = update??true;
		if(!pxi){
			//@ts-ignore
			$planet_GlobalDC_poof$.$planet_follow_cache_poof$.unshift([pxi_uri, key, call, update, np]);
		}else{
			//@ts-ignore
			np.$planet_resolve_poof$($planet_DataManager_poof$.$planet_follow_poof$(pxi, key, call, update));
		}

		return np.$planet_promise_poof$;
	}

	private static $planet__interval_call_token_map__poof$: Map<string, {timestamp: number, interval: number, call: (td: number, now: number)=>any, that?: any}> = new Map();

	public static $planet_interval_poof$(interval: number, call: (td: number, now: number)=>any, thisArg?: any){
		let token = abd.$planet_string_poof$.$planet_randomToken_poof$(7, 36, tlkn=>!$planet_GlobalDC_poof$.$planet__interval_call_token_map__poof$.has(tlkn));
		$planet_GlobalDC_poof$.$planet__interval_call_token_map__poof$.set(token, {timestamp: Date.now(), interval, call, that: thisArg??null})
		return token;
	}

	public static $planet_clearInterval_poof$(token: string){
		if(!$planet_GlobalDC_poof$.$planet__interval_call_token_map__poof$.has(token)) return void 0;

		$planet_GlobalDC_poof$.$planet__interval_call_token_map__poof$.delete(token);
	}
}
